package com.yazi.mxz.server.controller;

import java.util.List;

import javax.annotation.Resource;

import mo.framework.smh.hibernate.OrderType;
import mo.framework.smh.hibernate.PageInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonView;
import com.yazi.mxz.common.entity.beans.MemberNotice;
import com.yazi.mxz.common.entity.enums.MemberNoticeStatus;
import com.yazi.mxz.common.service.MemberNoticeService;
import com.yazi.mxz.server.response.IntegerResponse;
import com.yazi.mxz.server.response.MemberNoticeListResponse;
import com.yazi.mxz.server.response.StatusResponse;
import com.yazi.mxz.server.response.view.NoticeView;

@Controller("memberNoticeController")
@RequestMapping("/memberNotice")
public class MemberNoticeController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(MemberNoticeController.class);

	@Resource
	MemberNoticeService memberNoticeService;

	@RequestMapping("/count")
	@ResponseBody
	public IntegerResponse count() {
		log.debug("客户端请求 -> 获取最新通知数");
		IntegerResponse res = new IntegerResponse();
		try {
			MemberNotice memberNotice = new MemberNotice();
			memberNotice.setMember(getLoginMember());
			memberNotice.setStatus(MemberNoticeStatus.UNREAD.ordinal());
			int count = memberNoticeService.count(memberNotice, null);
			res.setSuccessData(count);
			res.setOk();
		} catch (Exception e) {
			log.error("客户端请求 -> 获取最新通知数时异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		return res;
	}

	@RequestMapping("/list")
	@ResponseBody
	@JsonView(NoticeView.class)
	public MemberNoticeListResponse list(@RequestBody PageInfo pageInfo){
		log.debug("客户端请求 ->　获取用户通知列表");
		MemberNoticeListResponse res = new MemberNoticeListResponse();
		try {
			Long memberId = getLoginMember().getMemberId();
			// 把用户所有通知更新"已查看"状态
			memberNoticeService.update(memberId);
			
			List<MemberNotice> mnList= memberNoticeService.searchByMemberId(memberId, pageInfo);
			
			res.setSuccessData(mnList);
			res.setPageInfo(pageInfo);
			res.setOkMessage("ok");
			
		} catch (Exception e) {
			log.error("客户端获取 ->　获取用户通知列表时异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		
		return res;
	}
	
	@RequestMapping("/removeDo")
	@ResponseBody
	public StatusResponse removeDo(@RequestBody Long[] noticeIds) {
		log.debug("客户端请求 -> 删除用户通知");
		StatusResponse res = new StatusResponse();
		try {
			Long memberId = getLoginMember().getMemberId();
			memberNoticeService.remove(memberId, noticeIds);
			res.setOkMessage("删除成功");
		} catch (Exception e) {
			log.error("客户端请求 -> 删除用户通知:", e);
			res.setErrorMessage(e.getMessage());
		}
		return res;
	}
}
